# The Effects of Lawn Signs on Vote Outcomes: 
# Results from Four Randomized Field Experiments
# Donald P. Green, Jonathan S. Krasno, Alexander Coppock, Benjamin D. Farrer, Brandon Lenoir, and Joshua N. Zingher

# In text calculations

# Uncomment to set your working directory
# setwd("")

# Uncomment to install required packages
# install.packages(c("stargazer", "xtable", "sandwich", "lmtest", "dplyr", "rmeta", "ggplot2"))

# Load Packages, Data, and Functions --------------------------------------

library(dplyr)
library(rmeta)
library(xtable)
library(ggplot2)
library(stargazer)

load("exp_1.rdata")
load("exp_2.rdata")
load("exp_3.rdata")
load("exp_4.rdata")

source("Lawn_Signs_Source.R")

exp_1 <- select(exp_1, margin, share, turnout, margin_cov, share_cov, turnout_cov,
                condition_factor, weights, include, study)
exp_2 <- select(exp_2, margin, share, turnout, margin_cov, share_cov, turnout_cov,
                condition_factor, weights, include, study)
exp_3 <- select(exp_3, margin, share, turnout, margin_cov, share_cov, turnout_cov,
                condition_factor, weights, include, study)
exp_4 <- select(exp_4, margin, share, turnout, margin_cov, share_cov, turnout_cov,
                condition_factor, weights, include, study)

all_data <- rbind(exp_1, exp_2, exp_3, exp_4)


##replication table two
# Ns table ---------------------------------------------------------------

ns_table <- 
all_data %>%
  filter(include ==1) %>%
  group_by(study) %>%
  summarize(Control = sum(condition_factor == "Control"),
         Adjacent = sum(condition_factor == "Adjacent"),
         Treated = sum(condition_factor == "Treated"),
         Total = n()) %>%
  rename(Study =study)

# print(xtable(ns_table, label = "tab: ns",caption="Treatment Assignments"), include.rownames = FALSE, caption.placement = "top",
#      file="ns_table.tex")


##replication footnote four
# Cost Calculations -------------------------------------------------------

total_turnout <- sum(all_data$turnout)
total_fx <- 0.017 + 0.015
direct_fx <- 0.017
direct_se <- 0.007


total_cost <- 
  1850 + 345 +
  1850 + 
  4000 + 
  5000 

cost_calculator <- function(fx){
  total_votes <- fx * total_turnout
  return(total_cost/total_votes)
}

cost_calculator(direct_fx)
cost_calculator(total_fx)

fx_draws <- rnorm(10000, mean=direct_fx, sd = direct_se)
costs <- sapply(X = fx_draws, FUN = cost_calculator)
quantile(costs, probs = c(0.025, 0.975))




# Effects on Turnout ------------------------------------------------------

load("exp_1.rdata")
load("exp_2.rdata")
load("exp_3.rdata")
load("exp_4.rdata")

exp_1_fit <- lm(totalcvotes12 ~ condition_factor + totalcvotes10 + totalcvotes08 + totalcvotes06 + totalpvotes08,
                weights=weights, data=subset(exp_1, treatable==1))

exp_2_fit <- lm(turnout ~ condition_factor + registereddems + turnout09 + turnout05, weights=weights, data=subset(exp_2, in_whole_exp==1))

exp_3_fit <- lm(turnout ~  condition_factor + turnout_2012 + turnout_2009, weights=weights, data=exp_3)

exp_4_fit <- lm(turnout ~ condition_factor +
                  GOVT2010 + USPT2008 + GOVT2006 + USPT2004 + GOVT2002 + USPT2000,
                weights = weights, data=exp_4)

exp_1_fit_r <- commarobust(exp_1_fit)
exp_2_fit_r <- commarobust(exp_2_fit)
exp_3_fit_r <- commarobust(exp_3_fit)
exp_4_fit_r <- commarobust(exp_4_fit)

direct_fx <- c(exp_1_fit_r[2,1], exp_2_fit_r[2,1], exp_3_fit_r[2,1], exp_4_fit_r[2,1])
direct_fx_se <- c(exp_1_fit_r[2,2], exp_2_fit_r[2,2], exp_3_fit_r[2,2], exp_4_fit_r[2,2])
indirect_fx <- c(exp_1_fit_r[3,1], exp_2_fit_r[3,1], exp_3_fit_r[3,1], exp_4_fit_r[3,1])
indirect_fx_se <- c(exp_1_fit_r[3,2], exp_2_fit_r[3,2], exp_3_fit_r[3,2], exp_4_fit_r[3,2])

direct_meta <- meta.summaries(direct_fx, direct_fx_se)
indirect_meta <- meta.summaries(indirect_fx, indirect_fx_se)

direct_meta$summary
direct_meta$se.summary



